<?php
$_SESSION['steps'][$_GET['action']] = false;
if($_SESSION['install_id']=='')
{
	displayError('Vous devez autoriser les variables de sessions');
}
if(filesize($_SESSION['pack_path']) != $_SESSION['pack_size'])
{
	displayError('Erreur lors du téléchargement du package (taille)');
}

if(md5_file($_SESSION['pack_path']) != $_SESSION['pack_md5'])
{
	displayError('Erreur lors du téléchargement du package (signature)');
}

include_once($temp_path.'/setup_archive.php5');

$tgz = new gzip_file($_SESSION['pack_path']);
$tgz->set_options(array('basedir' => $temp_path, 'overwrite' => 1));
list($errors, $n_succes) = $tgz->extract_files(array(array('setup/', '*'), array('', 'index.xml')));
$_SESSION['extracted'] = array_merge($_SESSION['extracted'], $n_succes);


$index = file_get_contents($temp_path.'index.xml');
$_SESSION['index']=$index;

if($index===false)
{
	displayError('Impossible de trouver le fichier index.xml dans le package');
}
$setup = new SimpleXMLElement($index);
if($setup['version']>'1.0')
{
	displayError('Version non prise en charge pour le package');
}
if($setup->application[0]=='')
{
	displayError('le tag application est absent du package');
}
if(file_exists($current_path.'/install_report.php5'))
{
	include($current_path.'/install_report.php5');
	
	if($setup->application[0]->name[0]!=$install_report['application_name'])
	{
		displayError('Une autre application a déjà été installé dans ce répértoire.');
	}
	$i_version = explode('.', $install_report['application_version']);
	$c_version = explode('.', $setup->application[0]->version[0]);
	$comp = 0;
	for($i=0; $i<4; $i++)
	{
		if(intval($i_version[$i])>intval($c_version[$i]))
		{
			$comp = 1;
			break;
		}
		elseif(intval($i_version[$i])<intval($c_version[$i]))
		{
			$comp = -1;
			break;
		}
	}
	if($comp==0)
	{
		displayError('Cette version est déjà installée sur votre machine');
	}
	if($comp>0)
	{
		displayError('Une version superieur a déjà été installée sur votre machine');
	}
	$_SESSION['conf_db']['host'] = $install_report['database_host'];
	$_SESSION['conf_db']['database'] = $install_report['database_database'];
	$_SESSION['conf_db']['login'] = $install_report['database_login'];
	$_SESSION['conf_db']['password'] = $install_report['database_password'];
	$_SESSION['conf_db']['prefix'] = $install_report['database_prefix'];

	$_SESSION['conf_root']['login'] = $install_report['root_login'];
	$_SESSION['conf_root']['password_ok'] = $install_report['root_password'];
}
else 
{
	$_SESSION['conf_root']['password_ok'] = '';
}
$screens_rev = array();
$screens = array();
if(count($setup->screens[0]->requirement)>0)
{
	$screens_rev['check_requirement'] = count($screens);
	$screens[] = 'check_requirement';
}
if($setup->screens[0]->licence[0]!='' || $setup->screens[0]->licence[0]['path']!='')
{
	$screens_rev['check_licence'] = count($screens);
	$screens[] = 'check_licence';
}
foreach ($setup->screens[0]->connection as $connection)
{
	switch($connection['type'])
	{
		case 'mysql':
			$screens_rev['conf_mysql'] = count($screens);
			$screens[] = 'conf_mysql';
			break;
		case 'root':
			$screens_rev['conf_root'] = count($screens);
			$screens[] = 'conf_root';
			break;
	}
}
if(count($setup->screens[0]->extractfile)>0)
{
	$screens_rev['copy_file'] = count($screens);
	$screens[] = 'copy_file';
}
if(count($setup->screens[0]->createtable)>0)
{
	$screens_rev['copy_table'] = count($screens);
	$screens[] = 'copy_table';
}

$screens_rev['end'] = count($screens);
$screens[] = 'end';



$_SESSION['screens'] = $screens;
$_SESSION['screens_rev'] = $screens_rev;

$screen_label = array('check_requirement' => 'Système requis'
, 'check_licence' => 'License'
, 'conf_mysql' => 'Base de données'
, 'conf_root' => 'Administrateur'
, 'copy_file' => 'Copie des fichiers'
, 'copy_table' => 'Création des tables'
, 'end' => 'Fin'
);
$li = '<li id="li_self" class="install_done">Analyse Setup</li><li id="li_pack" class="install_done">Recherche Package</li><li id="li_down" class="install_done">Téléchargement</li>';
foreach ($screens as $screen)
{
	$li.='<li id="li_'.$screen.'">'.$screen_label[$screen].'</li>';
}

$next_screen = $screens[0];
$_SESSION['steps'][$_GET['action']] = true;
?>
document.getElementById('install_box_container').className = '<?php echo strToJs(strtolower(str_replace(' ', '_', $setup->application[0]->name[0])))?>'

var object = document.createElement('LINK');
object.setAttribute('rel', 'stylesheet');
object.setAttribute('type', 'text/css');
object.setAttribute('href', './temp/<?php echo $setup->screens[0]['css']?>');
document.body.appendChild(object);

document.getElementById('install_menu_pages').innerHTML = '<?php echo strToJs($li)?>';
						
document.getElementById('install_content_title_product').innerHTML = '<?php echo strToJs($setup->application[0]->name[0])?> Installation'
document.getElementById('install_content_title_version').innerHTML = '<?php echo strToJs($setup->application[0]->version[0])?>'

document.getElementById('install_next').style.display='none'
document.getElementById('install_back').style.display='none'

document.getElementById('install_menu_page').innerHTML=document.getElementById('li_<?php echo $next_screen?>').innerHTML
document.getElementById('li_<?php echo $next_screen?>').className='install_doing'

ajax.sendAsync(page+'?action=check_requirement&uid='+new Date().getTime(), function(datas)
{
	evalAjax(datas)
})